package com.kun.portal.webframe.service;
import com.kun.portal.webframe.dao.MessagePushDao;
import com.kun.portal.webframe.po.MessagePushPo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018-09-10.
 * 消息推送
 */
@Service
public class MessagePushService {
    @Autowired
    private MessagePushDao messagePushDao;
    @Autowired
    private  EntityManager entityManager;

    public Page<MessagePushPo> findMg(String mgType , Boolean handle, String mgContent, Pageable pageable){
        Specification query = new Specification<MessagePushPo>() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root<MessagePushPo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<javax.persistence.criteria.Predicate> pre= new ArrayList<>();
                if(StringUtils.isNotBlank(mgContent)){
                    pre.add(cb.like(root.get("mgContent"), "%"+mgContent+"%"));
                }

                if(StringUtils.isNotBlank(mgType)){
                    pre.add(cb.equal(root.get("moduleType"), mgType));
                }

                if(null!=handle&&handle){
                    pre.add(cb.equal(root.get("handle"), false));
                }
                Predicate predicate = cb.and(pre.toArray(new Predicate[pre.size()]));
                return predicate;
            }
        };
        return messagePushDao.findAll(query,pageable);

    }
    public void saveMp(MessagePushPo messagePushPo){
        messagePushDao.save(messagePushPo);
    }
    public Long findNotHandelMg(Boolean handle,String mgType){
       return messagePushDao.findNotHandelMg(handle,mgType);
    }
}

